package com.user.chris.controller;

import com.common.chris.core.JedisCore;
import com.common.chris.util.BeanCopyUtil;
import com.github.pagehelper.PageInfo;
import com.user.chris.entity.po.UserPO;
import com.user.chris.entity.vo.TestVO;
import com.user.chris.entity.vo.UserVO;
import com.user.chris.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class TestController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private JedisCore jedisCore;

    @Autowired
    private DataSource dataSource;

    @Value("${server.port}")
    String a;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private UserService userService;

    @Value("${qwer}")  // 使用@NacosValue 失败
    private Integer qwer;

    private Jedis getJedis() {
        Jedis resource = jedisPool.getResource();
        return resource;
    }

    @PostConstruct
    public void init() {
        System.out.println(a);
    }


    @GetMapping("/aa")
    public void aa() {
        try (Jedis jedis = getJedis();) {
            jedis.set("hello","world");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/bb")
    public void bb() {
        System.out.println(dataSource);
    }

    @GetMapping("/list/user")
    public PageInfo<UserPO> listUser() {
        PageInfo<UserPO> pageInfo = userService.listUser();
        log.info("123:{}", 123);
        return pageInfo;
    }

    @GetMapping("/list/test01")
    public PageInfo listTest01() {
        PageInfo pageInfo = userService.listUser();
        List<UserPO> list1 = pageInfo.getList();
        List<UserVO> userVOS = BeanCopyUtil.copyListProperties(list1, UserVO::new);
        pageInfo.setList(userVOS);
        return pageInfo;
    }

    @GetMapping("/list/test02")
    public int listTest02() {
        return 22;
    }

    @GetMapping("/list/test03")
    public Integer listTest03() {
        int a = 22/0;
        return 22;
    }

    @GetMapping("/list/test04")
    public void listTest04() {
        jedisCore.executeJedis(jedis -> {
            jedis.set("123", "尼玛B");
            jedis.expire("123", 100);
            String s = jedis.get("123");
            System.out.println(s);
        });
    }


    @GetMapping("/list/test05")
    public String listTest05() throws InterruptedException { // 分布式锁redisson实例
        String key = "kkk";
        RLock lock = redissonClient.getLock(key);// 获取连接锁对象
        lock.lock();// 循环去setnx 拿锁 在表中存的是一个 hash的类型
        System.out.println("呵呵呵");
        Thread.sleep(30000);
        lock.unlock();// 删除key 释放锁
        return "哈哈哈";
    }

    @GetMapping("/test03")
    public String test03() {
        System.out.println(qwer);
        return "123";
    }


    @GetMapping("/test04")
    public Integer test04() {
        System.out.println(qwer);
        log.info("测试日志123");
        return 123;
    }

    @GetMapping("/test05")
    public PageInfo test05() {
        PageInfo<UserPO> pageInfo = userService.listUser05();
        return pageInfo;
    }

    @GetMapping("/test06")
    public Date test06() {
        return new Date();
    }

    @GetMapping("/test07")
    public TestVO test07(TestVO testVO) {
        // testVO.validate();
        return testVO;
    }

    @GetMapping("/test08")
    public Integer test08(Integer age) {
        System.out.println("进入方法了---");
        System.out.println(age);
        return age;
    }

}